const {areq} = require('../../utils/util.js');

let app = getApp();

Page({
  data:{
    src:'',
    loginId:'',
    showModel:'',
    phone_number:'',
    passwd:'',
    isPassword:true,
    isDeletePhone:false,
    isDeletePasswd:false,
    isDeleteCode:false,
    checked:false,//复选框
    code:'',
    sendCode:null,
    isTime:false,
    countDown:'60',//验证码

    //假数据（注册过的手机号）
    registered_phone_number:15108789203,
    registered_passwd:'alkjvqu2288.'
  },

  onShow(){
    this.setData({
        checked:false
    })
  },

  onLoad(options){
    //进来就将请求地址从本地服务器改为内网穿透
    let global = app.globalData;
    let localSrc = global.localSrc;
    
    this.setData({
      src:localSrc
    });

    const loginId = options.loginId;
    this.setData({
      loginId:loginId
    })
  },

  phoneInput(e){
    const phone_number = e.detail.value;
    this.setData({
      phone_number:phone_number,
      isDeletePhone:true
    });
  },

  passwdInput(e){
    const passwd = e.detail.value;
    this.setData({
      passwd:passwd,
      isDeletePasswd:true
    });
  },

  //清空手机号信息
  clearPhoneInput(){
    this.setData({
      phone_number:''
    })
  },
  
  //清空密码信息
  clearPasswdInput(){
    this.setData({
      passwd:''
    })
  },

  //显示密码
  showPasswd(){
    this.setData({
      isPassword:false
    })
  },

  //隐藏密码
  hiddenPasswd(){
    this.setData({
      isPassword:true
    })
  },
  
  //手机号输入框焦点
  phoneInputFocus(){
    this.setData({
      isDeletePhone:!this.data.isDeletePhone
    })
  },
  
  //密码输入框焦点
  passwdInputFocus(){
    this.setData({
      isDeletePasswd:!this.data.isDeletePasswd
    })
  },

  //验证码输入框焦点
  codeInputCode(){
    this.setData({
      isDeleteCode:!this.data.isDeleteCode
    })
  },

  //提交密码
  loginPasswd(e){
    const loginInfo = e.detail.value;
    const phone_number = Number(loginInfo.phone_number);
    const passwd = loginInfo.passwd;
    const registered_phone_number = this.data.registered_phone_number;
    const registered_passwd = this.data.registered_passwd;
    console.log(typeof(registered_phone_number));
    console.log(typeof(registered_passwd));
    if(phone_number === 0 || passwd === ''){
      wx.showToast({
        title: '请填写完整',
        icon:'error'
      });
    }else if(typeof(phone_number) !== 'number'){
      wx.showToast({
        title: '非法手机号',
        icon:'error'
      })
    }else if(registered_phone_number === phone_number && registered_passwd === passwd){
      wx.showToast({
        title: '登录成功',
        icon:'success'
      })
    }else{
      wx.showToast({
        title: '登录失败',
        icon:'error'
      });
    }
    console.log(loginInfo);
  },

  //复选框
  onChange(){
    this.setData({
      checked:!this.data.checked,
    });
  },

  codeInput(e){
    const code = e.detail.value;
    this.setData({
      code:code,
      isDeleteCode:true
    })
  },

  //清空验证码信息
  clearCodeInput(){
    this.setData({
      code:''
    })
  },

  //倒计时开始的方法
  startCountdown() {
    let self = this;
    let endTime = 60; // 设置倒计时结束时间为60秒
    const interval = setInterval(() => {
      endTime -= 1; // 每秒减少1
      if (endTime < 0) {
        clearInterval(interval);
        self.setData({
          countDown: '60',
          isTime:false
        });
      } else {
        let seconds = endTime.toString(); // 转换为字符串
        if (seconds.length < 2) seconds = '0' + seconds; // 补零
        self.setData({
          countDown: seconds
        });
      }
    }, 1000);
  },

    getVerificationCodeModal(){
      areq.get(`${this.data.src}` + `public/send/code`).then(res=>{
        if(res.ok){
          const sendCode = res.data.data;
          console.log(sendCode);
          this.setData({
            sendCode:res.data.data
          })
        }else{
          console.log('获取失败');
        }
      })
    },
  
  //获取验证码
  getCode(){
    const phone_number = Number(this.data.phone_number);
    const checked = this.data.checked;
    if(phone_number.toString().length !== 11 || typeof(phone_number) !== 'number'){
        wx.showToast({
          title: '非法手机号',
          icon:'error'
        })
    }else if(checked === false){
        this.setData({
          showModel:true
        })
    }else{
      this.setData({
        checked:true,
        isTime:true
      })
      this.getVerificationCodeModal();
      this.startCountdown();
    }
  },

  //使用验证码登录
  loginCode(e){
    const loginInfo = e.detail.value;
    const phone_number = Number(loginInfo.phone_number);
    const code = Number(loginInfo.code);
    const registered_phone_number =this.data.registered_phone_number;
    const sendCode = Number(this.data.sendCode);
    console.log(typeof(registered_phone_number));
    console.log(typeof(sendCode));
    console.log(phone_number.toString().length)
    if(phone_number === 0 || code === 0){
      wx.showToast({
        title: '请填写完整',
        icon:'error'
      });
    }else if(typeof(phone_number) !== 'number' || phone_number.toString().length !== 11){
        wx.showToast({
          title: '非法手机号格式',
          icon:'error'
        });
    }else if(typeof(code) !== 'number'){
      wx.showToast({
        title: '非法验证码格式',
        icon:'error'
      })
    }else if(registered_phone_number === phone_number && code === sendCode){
      wx.showToast({
        title: '登录成功',
        icon:'success'
      })
    }else{
      wx.showToast({
        title: '登录失败',
        icon:'error'
      })
    }
    console.log(loginInfo);
  },

  //跳转到注册界面
  routeToRegister(){
    console.log('注册');
    wx.navigateTo({
      url: '/pages/register/register',
    })
  },

  //模态框（不同意）
  onCancel(){
    this.setData({
      showModel:false
    })
  },

  //模态框（同意）
  onAgree(){
      const checked = this.data.checked;
      this.getVerificationCodeModal();
      this.setData({
          showModel:false
      }) 
      if(checked === false){
        this.setData({
          checked:true,
          isTime:true
        })
        this.startCountdown();
      }
  }
})